package org.firebug.spring.boot.starter;

import org.firebug.spring.boot.starter.service.*;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;

@Configuration
@Profile(value = {"local"})
@EnableConfigurationProperties(SpringBootStarterProperties.class)
@ConditionalOnProperty(name = "spring.boot.starter.enabled", havingValue = "true")
public class SpringBootStarterConfiguration {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Bean
    @ConditionalOnClass(name = {"org.springframework.data.redis.core.StringRedisTemplate"})
//    @ConditionalOnClass(value = {StringRedisTemplate.class}) // missing class -> java.lang.ArrayStoreException
    public ConditionalOnClassService ConditionalOnClassService() {
        return new ConditionalOnClassService();
    }

    @Bean
    @ConditionalOnMissingClass(value = {"org.springframework.data.redis.core.StringRedisTemplate"})
    public ConditionalOnMissingClassService conditionalOnMissingClassService() {
        return new ConditionalOnMissingClassService();
    }

    @Bean
    @ConditionalOnBean(name = {"org.springframework.data.redis.core.StringRedisTemplate"})
//    @ConditionalOnBean(value = {StringRedisTemplate.class}) // 加载顺序问题 -> @ConditionalOnClass
    public ConditionalOnBeanService conditionalOnBeanService() {
        return new ConditionalOnBeanService();
    }

    @Bean
    @ConditionalOnMissingBean(name = {"org.springframework.data.redis.core.StringRedisTemplate"})
//    @ConditionalOnMissingBean(value = {StringRedisTemplate.class}) // missing class -> java.lang.ArrayStoreException
    public ConditionalOnMissingBeanService conditionalOnMissingBeanService() {
        return new ConditionalOnMissingBeanService();
    }

    @Bean
    @ConditionalOnProperty(name = "spring.boot.starter.open", havingValue = "true")
    public ConditionalOnPropertyService conditionalOnPropertyService() {
        return new ConditionalOnPropertyService();
    }
}
